package com.atguigu.flink;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 有界数据流处理
 */
public class Demo02 {
    public static void main(String[] args) throws Exception {

        //创建流式执行环境
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> stringDataStreamSource = executionEnvironment.readTextFile("input/word.txt");
        
        //对数据进行处理
        SingleOutputStreamOperator<Tuple2<String, Long>> result = stringDataStreamSource.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                //对数据进行切分压平
                String[] s = value.split(" ");
                for (String str : s) {
                    out.collect(str);
                }
            }
        })
                //对数据进行变化，变为元组
                .map(new MapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> map(String value) throws Exception {
                        //将数据变为元组
                        return Tuple2.of(value, 1L);
                    }
                })
                //对数据进行分组
                .keyBy(new KeySelector<Tuple2<String, Long>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Long> value) throws Exception {
                        return value.f0;
                    }
                })
                //进行计算分组
                .sum(1);

        //结果展示
        result.print();

        //因为是流式处理，所以要提交，并阻塞进程处理
        executionEnvironment.execute();
    }
}
